package xyz.xiangyushui.controller.employeeLeader;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xyz.xiangyushui.dao.commodity.CommodityDao;
import xyz.xiangyushui.dao.salesPlan.SalesPlanDao;
import xyz.xiangyushui.javaBean.salesPlan.DailyPlan;
import xyz.xiangyushui.javaBean.salesPlan.DailyPlanCommodity;

/**
 * Servlet implementation class ModifySalesPlan
 */
@WebServlet("/employeeLeader/salesPlan")
public class ModifySalesPlan extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private SalesPlanDao spd;
	private CommodityDao cd;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifySalesPlan() {
        super();
        spd=new SalesPlanDao();
        cd=new CommodityDao();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String planId=request.getParameter("id");
		if(planId==null){
			response.sendError(404);
		}else{
			if(spd.exists(planId)){
				request.setAttribute("commodities", cd.getAllCommodityIdAndName());
				request.setAttribute("planId", planId);
				request.getRequestDispatcher("/employeeLeader/salesPlan/modify.jsp").forward(request, response);
			}else{
				response.sendError(404);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int storeId=(int) request.getSession(false).getAttribute("storeId");
		String date = request.getParameter("date");
		long dateLong = Long.parseLong(date);
		
		String[] commodity = request.getParameterValues("commodity");
		System.out.println(commodity.length);
		String[] num = request.getParameterValues("num");
		String[] price = request.getParameterValues("price");
		DailyPlan dp = new DailyPlan();

		List<DailyPlanCommodity> ls = new LinkedList<>();

		for (int i = 0; i < commodity.length; i++) {
			if (valid(commodity[i], num[i], price[i])) {
				DailyPlanCommodity dpc = new DailyPlanCommodity();
				dpc.setCommodityIdName(commodity[i]);
				dpc.setNum(Integer.parseInt(num[i]));
				dpc.setPrice(Double.parseDouble(price[i]));
				ls.add(dpc);
			}
		}
		dp.setDate(dateLong);
		dp.setCommodity(ls);
		dp.setStoreId(storeId);
		System.out.println(dp.getCommodity().size());

		spd.update(dp);
		response.sendRedirect(request.getContextPath()+"/employeeLeader");
	}
	
	private boolean valid(String commodity, String num, String price) {
		boolean valid = true;
		if (("".equals(commodity)) || ("".equals(num)) || ("".equals(price))) {
			return false;
		}
		try {
			Integer.parseInt(num);
			Double.parseDouble(price);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			valid = false;
		}

		return valid;
	}

}
